<?php

/**
 * Created by PhpStorm.
 * User: evan
 * Date: 2017/6/29
 * Time: 上午11:38
 */
class Im extends MY_Api_Controller
{
    public function live_end()
    {
        $ds_id  = (int)$this->input->get_post('ds_id');
        $minute = (int)$this->input->get_post('minute');


        if (!$ds_id)
        {
            $this->json_output(array(), 100002);
            //return sur(array(), 100002);
        }


        $this->load->library('im_api/TimRestApi');

        $api = createRestAPI();

        $ret = $api->generate_user_sig();
        if ($ret == null)
        {
            $this->json_output(array(), 170013);
            //return sur(array(), 170013);
            // 签名生成失败
            //echo '签名生成失败';
            //die();
        }


        $this->load->model('direct_seeding/Model_direct_seeding', 'ds');
        $ds_item = $this->ds->getById($ds_id);


        $return = array();

        if ($minute)
        {
            $remind_second = ( ($minute - 1) * 60 ) + rand(35, 60);//(strtotime($v['real_end_time']) - TIMESTAMP);
            $remind_minute = ceil($remind_second / 60);
            $remind_minute = $remind_minute > 5 ? 5 : $remind_minute;


            $message = array
            (
                'remind_second' => $remind_second,
                'remind_minute' => $remind_minute,
            );


            $ret = $api->group_send_group_msg("admin", $ds_item['chat_room_num'], "@MSMK@LIVING_WILL_END@".json_encode($message));
            $return[] = array
            (
                'message_field'   =>'@MSMK@LIVING_WILL_END@',
                'im_opera_status' => $ret,
                'message_content' => $message,
            );


            if (1 == $remind_minute)
            {
                //30秒以后再再同步一次
                sleep(30);


                $message['remind_second'] = $remind_second - 30;
                $ret = $api->group_send_group_msg("admin", $ds_item['chat_room_num'], "@MSMK@LIVING_WILL_END@".json_encode($message));
                $return[] = array
                (
                    'message_field'   =>'@MSMK@LIVING_WILL_END@',
                    'im_opera_status' => $ret,
                    'message_content' => $message,
                );
            }
        }
        else
        {
            $ret = $api->group_send_group_msg("admin", $ds_item['chat_room_num'], "@MSMK@LIVING_ALREADY_END@");
            $return[] = array
            (
                'message_field'   =>'@MSMK@LIVING_WILL_END@',
                'im_opera_status' => $ret,
                'message_content' => '',
            );
            //echo "通知主播结束IM消息发送状态(@MSMK@LIVING_ALREADY_END@)：\n";print_r($ret);
        }


        $this->json_output($return);
        //return sur($return);
    }
}